package com.neusoft.yuejian12.yuejian_12.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.adapter.ActivityListAdapter;
import com.neusoft.yuejian12.yuejian_12.javabean.ActivityBean;
import com.neusoft.yuejian12.yuejian_12.model.ActivityModel;

import java.security.Permissions;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.R.attr.fragment;
import static android.R.attr.priority;
import static com.neusoft.yuejian12.yuejian_12.R.id.shouye_professor;

/**
 * Created by Administrator on 2017/9/26.
 */

public class ShouyeFragment extends Fragment implements View.OnClickListener{

   // 声明变量
    private View view = null;
    private ImageView imagesearch;//搜索
    private RollPagerView rollPVshouye = null;

    private RadioButton activityRb;
    private RadioButton professorRb;
    private RadioButton matchRb;

    private Fragment activityFragment;
    private Fragment professorFragment;
    private Fragment matchFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
       view =  inflater.inflate(R.layout.tab03, container, false);
        initViews();
       // getData();
        initEvents();
        return view;
    }
    private void initViews(){
        imagesearch = (ImageView) view.findViewById(R.id.shouye_pic);
        rollPVshouye = (RollPagerView) view.findViewById(R.id.rollPV_shouye);
        activityRb = (RadioButton) view.findViewById(R.id.shouye_activity);
        professorRb = (RadioButton) view.findViewById(R.id.shouye_professor);
        matchRb = (RadioButton) view.findViewById(R.id.shouye_match);
    }
    //轮播图
    public void initEvents() {

        //跳转到搜索页面
//        imagesearch.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(ShouyeFragment.this,ShouyeSearchFragment.class);
//                startActivityForResult(intent,1);
//            }
//        });
        rollPVshouye.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPVshouye.setAnimationDurtion(500);
        //设置指示器：
        //rollPV.setHintView(new IconHintView());
        //rollPV.setHintView(new IconHintView(this,R.mipmap.ic_launcher,R.mipmap.ic_launcher));
        rollPVshouye.setHintView(new ColorPointHintView(getActivity(),
                getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPVshouye.setAdapter(new RollPagerAdapter());
        //设置每一个图片的点击事件
        /*rollPV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(getActivity(), "点击了图片"+position, Toast.LENGTH_SHORT).show();

            }
        });*/

        //首页三个列表
        //精选活动页
        activityRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activityFragment = new ActivityFragment();
                    replaceFragment(activityFragment);
                }
            }
        });
        //活动大师
        professorRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    professorFragment = new ProfessorFragment();
                    replaceFragment(professorFragment);
                }

            }
        });
        //最近比赛
        matchRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    matchFragment = new MatchFragment();
                    replaceFragment(matchFragment);
                }
            }
        });
        fragmentManager=getChildFragmentManager();
        activityRb.setChecked(true);

    }
        private void replaceFragment(Fragment fragment) {
            transaction=fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment_holder,fragment);
            transaction.commit();
        }
        public ShouyeFragment(){

        }

    private int images[] = {
            R.drawable.shouye_1,
            R.drawable.shouye_2,
            R.drawable.shouye_3
    };

    @Override
    public void onClick(View view) {

    }

    //设置适配器
    private class RollPagerAdapter extends StaticPagerAdapter{

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView view = new ImageView(container.getContext());
        //设置图片资源
        view.setImageResource(images[position]);
        //设置高度和宽度
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        //设置拉伸方式
        view.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return view;
    }
    @Override
    public int getCount() {
        return images.length;
    }
}
//获取列表数据
//    private void getData(){
//        ActivityModel activityModel = new ActivityModel();
//        Call<List<ActivityBean>> call=activityModel.activitylist();
//        call.enqueue(new Callback<List<ActivityBean>>() {
//            @Override
//            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
//
//                List<ActivityBean> data = response.body();//获取数据
//                System.out.print("data");
//               // recyclerView.setAdapter(new ActivityListAdapter(ShouyeFragment.this.getActivity(), data));
//            }
//
//            @Override
//            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
//
//
//            }
//        });

    }

